﻿using NPOI.HSSF.Util;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using NPOI.XSSF.UserModel;
using System.Data;

namespace tools.office.npoi
{
    /// <summary>
    /// NPOI Excel 导出工具类
    /// </summary>
    public class NpoiExcelExportTool
    {
        /// <summary>
        /// 快速导出
        /// </summary>
        /// <param name="dataTable">数据</param>
        /// <param name="titles">标题集（默认使用 DataTable 的列名）</param>
        /// <returns>Excel 文件流</returns>
        public static MemoryStream FastExport(DataTable dataTable, string[]? titles = null)
        {
            int columnCount;

            var workbook = new XSSFWorkbook();
            var sheet = workbook.CreateSheet();

            #region 创建标题

            var titleRow = sheet.CreateRow(0);

            if (titles != null)
            {
                columnCount = titles.Length;

                for (int i = 0; i < columnCount; i++)
                {
                    titleRow.CreateCell(i).SetCellValue(titles[i]);
                }
            }
            else
            {
                columnCount = dataTable.Columns.Count;

                for (int i = 0; i < columnCount; i++)
                {
                    titleRow.CreateCell(i).SetCellValue(dataTable.Columns[i].ColumnName);
                }
            }

            #endregion

            #region 创建数据

            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                var dataRow = dataTable.Rows[i];
                var cellRow = sheet.CreateRow(i + 1);

                for (int j = 0; j < columnCount; j++)
                {
                    var dataCell = cellRow.CreateCell(j);
                    CreateTableHelper.SetCellValue(dataCell, dataRow[j]);
                }
            }

            #endregion

            var ms = new MemoryStream();
            workbook.Write(ms, true);

            ms.Position = 0;

            return ms;
        }

        public static MemoryStream HandmadeExport(
            DataTable dataTable, Dictionary<int, string> typeDict, string stocksite)
        {
            string sheetName = DateTime.Today.ToString("M月d日计划");

            XSSFWorkbook workbook = new();
            ISheet sheet = workbook.CreateSheet(sheetName);

            #region 样式

            // 标题行样式
            IFont titleRowFont = FontHelper.GetFont(workbook, "黑体", 10, true);
            ICellStyle titleRowDefaultStyle = CellStyleHelper.GetCellStyle(
                workbook, titleRowFont, HorizontalAlignment.Center, true, true);

            ICellStyle titleRowColoredStyle = CellStyleHelper.FillForegroundColor(
                workbook, titleRowDefaultStyle, [253, 233, 217]);

            ICellStyle titleRowNoBorderStyle = CellStyleHelper.GetCellStyle(
                workbook, titleRowFont, HorizontalAlignment.Center);

            // 数据行样式
            IFont dataRowFont = FontHelper.GetFont(workbook, "黑体", 10);
            ICellStyle dataRowDefaultStyle = CellStyleHelper.GetCellStyle(
                workbook, dataRowFont, HorizontalAlignment.Center, displayBorder: true);

            ICellStyle dataRowColoredStyle = CellStyleHelper.FillForegroundColor(
                workbook, dataRowDefaultStyle, [253, 233, 217]);

            #endregion

            #region 标题行

            RowInfo startRow = new() { Index = 1, Height = 20.1f };
            RowInfo endRow = new() { Index = 2, Height = 42f };
            List<TwoRowTitleColumnInfo> titleColumnConfigs =
            [
                new()
                {
                    Title = "序号",
                    RowSpan = 2,
                    Width = 15
                },
                new()
                {
                    Title = "部品信息",
                    ColSpan = 4,
                    SubTitles =
                    [
                        new(){ Title = "全球号", Width = 10 },
                        new(){ Title = "图号", Width = 10 },
                        new(){ Title = "品名", Width = 10 },
                        new(){ Title = "规格描述", Width = 12 }
                    ]
                },
                new()
                {
                    Title = $"裸件在库\n({stocksite})",
                    RowSpan = 2,
                    Width = 10
                },
                new()
                {
                    Title = "成品在库\n(C13)",
                    RowSpan = 2,
                    Width = 10
                },
                new()
                {
                    Title = "包装数量",
                    RowSpan = 2,
                    Width = 10,
                    IsColored = true
                },
                new()
                {
                    Title = "包装前\n点检",
                    RowSpan = 2,
                    Width = 10,
                    IsColored = true
                },
                new()
                {
                    Title = "包装信息",
                    ColSpan = 2,
                    SubTitles =
                    [
                        new(){ Title = "净重G", Width = 10 },
                        new(){ Title = "毛重G", Width = 10 }
                    ]
                },
                new()
                {
                    Title = "包装信息/辅材信息",
                    ColSpan = typeDict.Count,
                    SubTitles = [.. typeDict.Select(type => new ColInfo
                    {
                        Title = type.Value,
                        Width = 10
                    })]
                },
                new()
                {
                    Title = "其它信息",
                    ColSpan = 5,
                    IsColored = true,
                    SubTitles =
                    [
                        new(){ Title = "适配器", Width = 10 },
                        new(){ Title = "防伪\n标签", Width = 10 },
                        new(){ Title = "说明书", Width = 10 },
                        new(){ Title = "照片", Width = 10 },
                        new(){ Title = "海外", Width = 10 }
                    ]
                },
                new()
                {
                    Title = "包装完成\n确认",
                    RowSpan = 2,
                    Width = 15,
                    IsColored = true
                },
                new()
                {
                    Title = "品质\n抽检",
                    RowSpan = 2,
                    Width = 15,
                    IsColored = true
                }
            ];

            int lastColumnIndex = CreateTableHelper.CreateTitleRowWithTwoRow(
                sheet, startRow, endRow, titleColumnConfigs, titleRowDefaultStyle, titleRowColoredStyle);

            // 首行
            IRow row0 = sheet.CreateRow(0);
            row0.HeightInPoints = 20.1f;

            ICell cellA1 = row0.CreateCell(0);
            cellA1.SetCellValue("部品个装作业表");
            cellA1.CellStyle = titleRowNoBorderStyle;

            ICell cellLast1 = row0.CreateCell(lastColumnIndex - 1);
            cellLast1.SetCellValue(sheetName);
            cellLast1.CellStyle = titleRowNoBorderStyle;

            #endregion

            #region 数据行

            RowInfo dataRowStartRow = new() { Index = 3, Height = 20.1f };
            List<DataColumnInfo> dataColumnConfigs =
            [
                new() { FieldName = "number" },
                new() { FieldName = "productnumber" },
                new() { FieldName = "daikin_drawingno" },
                new() { FieldName = "name" },
                new() { FieldName = "new_specification" },
                new() { FieldName = "daikin_check_accept" },
                new() { FieldName = "finished_products" },
                new() { IsColored = true },
                new() { IsColored = true },
                new() { FieldName = "daikin_wt" },
                new() { FieldName = "daikin_capacitywt" },
                new() { FieldName = "bc_infos", ColSpan = typeDict.Count },
                new() { IsColored = true },
                new() { IsColored = true },
                new() { IsColored = true },
                new() { IsColored = true },
                new() { IsColored = true },
                new() { IsColored = true },
                new() { IsColored = true } 
            ];

            int remarkRowIndex = CreateTableHelper.CreateDataRow(
                sheet, dataRowStartRow, dataTable, dataColumnConfigs, dataRowDefaultStyle, dataRowColoredStyle);

            #endregion

            #region 备注行

            // 样式
            IFont redFont = FontHelper.GetFont(workbook, "黑体", 10, true, HSSFColor.Red.Index);
            ICellStyle remarkStyle = CellStyleHelper.GetCellStyle(workbook, redFont, HorizontalAlignment.Left);
            ICellStyle remarkRightStyle = CellStyleHelper.GetCellStyle(workbook, redFont, HorizontalAlignment.Right);

            ICellStyle bottomBorderStyle = workbook.CreateCellStyle();
            bottomBorderStyle.CloneStyleFrom(titleRowNoBorderStyle);
            bottomBorderStyle.BorderBottom = BorderStyle.Thin;

            float defaultRowHeight = 20f;
            int totalColumns = lastColumnIndex;
            int lastSevenStartCol = totalColumns - 7;

            // ==================== 第一行备注 ====================
            IRow remarkRow = sheet.CreateRow(remarkRowIndex);
            remarkRow.HeightInPoints = defaultRowHeight;

            // 左侧备注区域
            CellRangeAddress remarkMerge = new(remarkRowIndex, remarkRowIndex, 1, 3);
            sheet.AddMergedRegion(remarkMerge);
            ICell remarkCell = remarkRow.CreateCell(1);
            remarkCell.SetCellValue("1.标绿色部品是优先对应加急部品");
            remarkCell.CellStyle = remarkStyle;

            // 中间区域 - 品质确认要求
            CellRangeAddress beforeMerge = new(remarkRowIndex, remarkRowIndex, lastSevenStartCol - 2, lastSevenStartCol - 1);
            sheet.AddMergedRegion(beforeMerge);
            ICell beforeCell = remarkRow.CreateCell(lastSevenStartCol - 2);
            beforeCell.SetCellValue("品质确认要求：");
            beforeCell.CellStyle = remarkRightStyle;

            // 右侧备注区域
            CellRangeAddress lastSevenMerge = new(remarkRowIndex, remarkRowIndex, lastSevenStartCol, totalColumns - 1);
            sheet.AddMergedRegion(lastSevenMerge);
            ICell lastSevenCell = remarkRow.CreateCell(lastSevenStartCol);
            lastSevenCell.SetCellValue("1.防伪标签——粘贴在平整区域,不得覆盖部品原有规格信息上");
            lastSevenCell.CellStyle = remarkStyle;

            remarkRowIndex++;

            // ==================== 第二行备注 ====================
            IRow remarkRow2 = sheet.CreateRow(remarkRowIndex);
            remarkRow2.HeightInPoints = defaultRowHeight;

            // 左侧备注区域
            CellRangeAddress remarkMerge2 = new(remarkRowIndex, remarkRowIndex, 1, 6);
            sheet.AddMergedRegion(remarkMerge2);
            ICell remarkCell2 = remarkRow2.CreateCell(1);
            remarkCell2.SetCellValue("2.登记空缺的部品规格，净重，毛重，辅材信息，长宽高变更信息");
            remarkCell2.CellStyle = remarkStyle;

            // 右侧备注区域
            CellRangeAddress lastSevenMerge2 = new(remarkRowIndex, remarkRowIndex, lastSevenStartCol, totalColumns - 2);
            sheet.AddMergedRegion(lastSevenMerge2);
            ICell lastSevenCell2 = remarkRow2.CreateCell(lastSevenStartCol);
            lastSevenCell2.SetCellValue("2.辅材填充——根据辅材清单填充，封箱后部品不晃动。");
            lastSevenCell2.CellStyle = remarkStyle;

            remarkRowIndex++;

            // ==================== 第三行备注 ====================
            IRow remarkRow3 = sheet.CreateRow(remarkRowIndex);
            remarkRow3.HeightInPoints = defaultRowHeight;

            // 左侧备注区域
            CellRangeAddress remarkMerge3 = new(remarkRowIndex, remarkRowIndex, 1, 3);
            sheet.AddMergedRegion(remarkMerge3);
            ICell remarkCell3 = remarkRow3.CreateCell(1);
            remarkCell3.SetCellValue("3.需拍照的部品请当天完成提交");
            remarkCell3.CellStyle = remarkStyle;

            // 右侧备注区域
            CellRangeAddress lastSevenMerge3 = new(remarkRowIndex, remarkRowIndex, lastSevenStartCol, totalColumns - 2);
            sheet.AddMergedRegion(lastSevenMerge3);
            ICell lastSevenCell3 = remarkRow3.CreateCell(lastSevenStartCol);
            lastSevenCell3.SetCellValue("3.封箱带———胶带需平整，不翘起，上下对齐");
            lastSevenCell3.CellStyle = remarkStyle;

            remarkRowIndex++;

            // ==================== 第四行备注 ====================
            IRow remarkRow4 = sheet.CreateRow(remarkRowIndex);
            remarkRow4.HeightInPoints = defaultRowHeight;

            // 左侧备注区域
            CellRangeAddress remarkMerge4 = new(remarkRowIndex, remarkRowIndex, 1, 3);
            sheet.AddMergedRegion(remarkMerge4);
            ICell remarkCell4 = remarkRow4.CreateCell(1);
            remarkCell4.SetCellValue("4.海外部品不需要说明书，适配器");
            remarkCell4.CellStyle = remarkStyle;

            // 右侧备注区域
            CellRangeAddress lastSevenMerge4 = new(remarkRowIndex, remarkRowIndex, lastSevenStartCol, totalColumns - 1);
            sheet.AddMergedRegion(lastSevenMerge4);
            ICell lastSevenCell4 = remarkRow4.CreateCell(lastSevenStartCol);
            lastSevenCell4.SetCellValue("4.标签————粘贴应平整、不歪斜、不翘起、标签表面无损伤、无污染");
            lastSevenCell4.CellStyle = remarkStyle;

            remarkRowIndex++;

            // ==================== 第五行备注 ====================
            IRow remarkRow5 = sheet.CreateRow(remarkRowIndex);
            remarkRow5.HeightInPoints = defaultRowHeight;

            // 左侧备注区域
            CellRangeAddress remarkMerge5 = new(remarkRowIndex, remarkRowIndex, 1, 6);
            sheet.AddMergedRegion(remarkMerge5);
            ICell remarkCell5 = remarkRow5.CreateCell(1);
            remarkCell5.SetCellValue("5.无托盘的蜂窝板箱捆包作业时同一位置需打两条捆包带固定");
            remarkCell5.CellStyle = remarkStyle;

            // 中间区域 - 信息采集
            CellRangeAddress beforeMerge5 = new(remarkRowIndex, remarkRowIndex, lastSevenStartCol - 2, lastSevenStartCol - 1);
            sheet.AddMergedRegion(beforeMerge5);
            ICell beforeCell5 = remarkRow5.CreateCell(lastSevenStartCol - 2);
            beforeCell5.SetCellValue("信息采集：");
            beforeCell5.CellStyle = remarkRightStyle;

            // 右侧备注区域
            CellRangeAddress lastSevenMerge5 = new(remarkRowIndex, remarkRowIndex, lastSevenStartCol, totalColumns - 6);
            sheet.AddMergedRegion(lastSevenMerge5);
            ICell lastSevenCell5 = remarkRow5.CreateCell(lastSevenStartCol);
            lastSevenCell5.SetCellValue("1.部品照片");
            lastSevenCell5.CellStyle = remarkStyle;

            remarkRowIndex++;

            // ==================== 第六行备注 ====================
            IRow remarkRow6 = sheet.CreateRow(remarkRowIndex);
            remarkRow6.HeightInPoints = defaultRowHeight;

            // 左侧备注区域
            CellRangeAddress remarkMerge6 = new(remarkRowIndex, remarkRowIndex, 1, 4);
            sheet.AddMergedRegion(remarkMerge6);
            ICell remarkCell6 = remarkRow6.CreateCell(1);
            remarkCell6.SetCellValue("6.压缩机完成后需当日提交压缩机号");
            remarkCell6.CellStyle = remarkStyle;

            // 右侧上部区域 - 确认规格
            CellRangeAddress lastSevenMerge6 = new(remarkRowIndex, remarkRowIndex, lastSevenStartCol, totalColumns - 6);
            sheet.AddMergedRegion(lastSevenMerge6);
            ICell lastSevenCell6 = remarkRow6.CreateCell(lastSevenStartCol);
            lastSevenCell6.SetCellValue("2.确认规格");
            lastSevenCell6.CellStyle = remarkStyle;

            // 右侧中部区域 - 品质点检确认
            CellRangeAddress qualityCheckMerge = new(remarkRowIndex, remarkRowIndex, totalColumns - 4, totalColumns - 2);
            sheet.AddMergedRegion(qualityCheckMerge);
            ICell qualityCheckCell = remarkRow6.CreateCell(totalColumns - 4);
            qualityCheckCell.SetCellValue("品质点检确认");
            qualityCheckCell.CellStyle = titleRowNoBorderStyle;

            // 右侧下部单元格 - 带边框
            ICell bottomCell1 = remarkRow6.CreateCell(totalColumns - 2);
            bottomCell1.CellStyle = bottomBorderStyle;
            ICell bottomCell2 = remarkRow6.CreateCell(totalColumns - 1);
            bottomCell2.CellStyle = bottomBorderStyle;

            remarkRowIndex++;

            // ==================== 第七行备注 ====================
            IRow remarkRow7 = sheet.CreateRow(remarkRowIndex);
            remarkRow7.HeightInPoints = defaultRowHeight;

            // 左侧备注区域
            CellRangeAddress remarkMerge7 = new(remarkRowIndex, remarkRowIndex, 1, 11);
            sheet.AddMergedRegion(remarkMerge7);
            ICell remarkCell7 = remarkRow7.CreateCell(1);
            remarkCell7.SetCellValue("7.每日巡检担当需对当日包装品质进行抽检，抽检比例为当日计划总类的30%以上。确认品质达标需在品质抽检项√。");
            remarkCell7.CellStyle = remarkStyle;

            // 右侧中部区域 - 作业完成签名
            CellRangeAddress signMerge = new(remarkRowIndex, remarkRowIndex, totalColumns - 4, totalColumns - 2);
            sheet.AddMergedRegion(signMerge);
            ICell signCell = remarkRow7.CreateCell(totalColumns - 4);
            signCell.SetCellValue("作业完成签名");
            signCell.CellStyle = titleRowNoBorderStyle;

            // 右侧下部单元格 - 下边框
            ICell signBottomCell1 = remarkRow7.CreateCell(totalColumns - 2);
            signBottomCell1.CellStyle = bottomBorderStyle;
            ICell signBottomCell2 = remarkRow7.CreateCell(totalColumns - 1);
            signBottomCell2.CellStyle = bottomBorderStyle;

            remarkRowIndex++;

            // ==================== 第八行备注 ====================
            IRow remarkRow8 = sheet.CreateRow(remarkRowIndex);
            remarkRow8.HeightInPoints = defaultRowHeight;

            // 左侧备注区域
            CellRangeAddress remarkMerge8 = new(remarkRowIndex, remarkRowIndex, 1, 4);
            sheet.AddMergedRegion(remarkMerge8);
            ICell remarkCell8 = remarkRow8.CreateCell(1);
            remarkCell8.SetCellValue("8.标橙色部品包装时需核对热交上的线圈规格");
            remarkCell8.CellStyle = remarkStyle;

            #endregion

            var ms = new MemoryStream();
            workbook.Write(ms, true);

            ms.Position = 0;

            return ms;
        }

        public static MemoryStream TemplateExport(
            DataTable dataTable, Dictionary<int, string> typeDict, string stocksite)
        {
            byte[] templateBytes = File.ReadAllBytes("Templates/待包装裸件模板.xlsx");

            XSSFWorkbook workbook;
            using (MemoryStream templateFs = new(templateBytes))
            {
                workbook = new XSSFWorkbook(templateFs);
            }

            string sheetName = DateTime.Today.ToString("M月d日计划");
            workbook.SetSheetName(0, sheetName);

            float defaultRowHeight = 20f;
            int titleRows = 3;
            int dataRows = dataTable.Rows.Count;
            int remarkRows = 8;

            // 下移模板中的备注文字
            ISheet sheet = workbook.GetSheetAt(0);
            sheet.ShiftRows(0, remarkRows - 1, titleRows + dataRows);

            #region 样式

            // 标题行样式
            IFont titleRowFont = FontHelper.GetFont(workbook, "黑体", 10, true);
            ICellStyle titleRowDefaultStyle = CellStyleHelper.GetCellStyle(
                workbook, titleRowFont, HorizontalAlignment.Center, true, true);

            ICellStyle titleRowColoredStyle = CellStyleHelper.FillForegroundColor(
                workbook, titleRowDefaultStyle, [253, 233, 217]);

            ICellStyle titleRowNoBorderStyle = CellStyleHelper.GetCellStyle(
                workbook, titleRowFont, HorizontalAlignment.Center);

            // 数据行样式
            IFont dataRowFont = FontHelper.GetFont(workbook, "黑体", 10);
            ICellStyle dataRowDefaultStyle = CellStyleHelper.GetCellStyle(
                workbook, dataRowFont, HorizontalAlignment.Center, displayBorder: true);

            ICellStyle dataRowColoredStyle = CellStyleHelper.FillForegroundColor(
                workbook, dataRowDefaultStyle, [253, 233, 217]);

            #endregion

            #region 标题行

            RowInfo startRow = new() { Index = 1, Height = defaultRowHeight };
            RowInfo endRow = new() { Index = 2, Height = 42f };
            List<TwoRowTitleColumnInfo> titleColumnConfigs =
            [
                new()
                {
                    Title = "序号",
                    RowSpan = 2,
                    Width = 15
                },
                new()
                {
                    Title = "部品信息",
                    ColSpan = 4,
                    SubTitles =
                    [
                        new(){ Title = "全球号", Width = 10 },
                        new(){ Title = "图号", Width = 10 },
                        new(){ Title = "品名", Width = 10 },
                        new(){ Title = "规格描述", Width = 12 }
                    ]
                },
                new()
                {
                    Title = $"裸件在库\n({stocksite})",
                    RowSpan = 2,
                    Width = 10
                },
                new()
                {
                    Title = "成品在库\n(C13)",
                    RowSpan = 2,
                    Width = 10
                },
                new()
                {
                    Title = "包装数量",
                    RowSpan = 2,
                    Width = 10,
                    IsColored = true
                },
                new()
                {
                    Title = "包装前\n点检",
                    RowSpan = 2,
                    Width = 10,
                    IsColored = true
                },
                new()
                {
                    Title = "包装信息",
                    ColSpan = 2,
                    SubTitles =
                    [
                        new(){ Title = "净重G", Width = 10 },
                        new(){ Title = "毛重G", Width = 10 }
                    ]
                },
                new()
                {
                    Title = "包装信息/辅材信息",
                    ColSpan = typeDict.Count,
                    SubTitles = [.. typeDict.Select(type => new ColInfo
                    {
                        Title = type.Value,
                        Width = 10
                    })]
                },
                new()
                {
                    Title = "其它信息",
                    ColSpan = 5,
                    IsColored = true,
                    SubTitles =
                    [
                        new(){ Title = "适配器", Width = 10 },
                        new(){ Title = "防伪\n标签", Width = 10 },
                        new(){ Title = "说明书", Width = 10 },
                        new(){ Title = "照片", Width = 10 },
                        new(){ Title = "海外", Width = 10 }
                    ]
                },
                new()
                {
                    Title = "包装完成\n确认",
                    RowSpan = 2,
                    Width = 15,
                    IsColored = true
                },
                new()
                {
                    Title = "品质\n抽检",
                    RowSpan = 2,
                    Width = 15,
                    IsColored = true
                }
            ];

            int lastColumnIndex = CreateTableHelper.CreateTitleRowWithTwoRow(
                sheet, startRow, endRow, titleColumnConfigs, titleRowDefaultStyle, titleRowColoredStyle);

            // 首行
            IRow row0 = sheet.CreateRow(0);
            row0.HeightInPoints = defaultRowHeight;

            ICell cellA1 = row0.CreateCell(0);
            cellA1.SetCellValue("部品个装作业表");
            cellA1.CellStyle = titleRowNoBorderStyle;

            ICell cellLast1 = row0.CreateCell(lastColumnIndex - 1);
            cellLast1.SetCellValue(sheetName);
            cellLast1.CellStyle = titleRowNoBorderStyle;

            #endregion

            #region 数据行

            RowInfo dataRowStartRow = new() { Index = 3, Height = defaultRowHeight };
            List<DataColumnInfo> dataColumnConfigs =
            [
                new() { FieldName = "number" },
                new() { FieldName = "productnumber" },
                new() { FieldName = "daikin_drawingno" },
                new() { FieldName = "name" },
                new() { FieldName = "new_specification" },
                new() { FieldName = "daikin_check_accept" },
                new() { FieldName = "finished_products" },
                new() { IsColored = true },
                new() { IsColored = true },
                new() { FieldName = "daikin_wt" },
                new() { FieldName = "daikin_capacitywt" },
                new() { FieldName = "bc_infos", ColSpan = typeDict.Count },
                new() { IsColored = true },
                new() { IsColored = true },
                new() { IsColored = true },
                new() { IsColored = true },
                new() { IsColored = true },
                new() { IsColored = true },
                new() { IsColored = true }
            ];

            int remarkRowIndex = CreateTableHelper.CreateDataRow(
                sheet, dataRowStartRow, dataTable, dataColumnConfigs, dataRowDefaultStyle, dataRowColoredStyle);

            #endregion

            int count = 0;
            while (count < remarkRows)
            {
                // 重新设置行高（处理下移导致行高变为默认值问题）
                IRow remarkRow = sheet.GetRow(remarkRowIndex + count);
                remarkRow.HeightInPoints = defaultRowHeight;

                count++;
            }

            var ms = new MemoryStream();
            workbook.Write(ms, true);

            ms.Position = 0;

            return ms;
        }

        public static MemoryStream SimpleExport(DataTable dataTable, string stocksite)
        {
            string sheetName = DateTime.Today.ToString("M月d日计划");

            XSSFWorkbook workbook = new();
            ISheet sheet = workbook.CreateSheet(sheetName);

            #region 样式

            // 标题行样式
            IFont titleRowFont = FontHelper.GetFont(workbook, "黑体", 10, true);
            ICellStyle titleRowDefaultStyle = CellStyleHelper.GetCellStyle(
                workbook, titleRowFont, HorizontalAlignment.Center, true, true);

            ICellStyle titleRowColoredStyle = CellStyleHelper.FillForegroundColor(
                workbook, titleRowDefaultStyle, [253, 233, 217]);

            // 数据行样式
            IFont dataRowFont = FontHelper.GetFont(workbook, "黑体", 10);
            ICellStyle dataRowDefaultStyle = CellStyleHelper.GetCellStyle(
                workbook, dataRowFont, HorizontalAlignment.Center, displayBorder: true);

            ICellStyle dataRowColoredStyle = CellStyleHelper.FillForegroundColor(
                workbook, dataRowDefaultStyle, [253, 233, 217]);

            #endregion

            #region 标题行

            RowInfo startRow = new() { Index = 0, Height = 42f };
            List<OneRowTitleColumnInfo> titleColumnConfigs =
            [
                new()
                {
                    Title = "序号",
                    Width = 15
                },
                new()
                {
                    Title = "全球号",
                    Width = 10
                },
                new()
                {
                    Title = "图号",
                    Width = 10
                },
                new()
                {
                    Title = "品名",
                    Width = 10
                },
                new()
                {
                    Title = "规格描述",
                    Width = 25
                },
                new()
                {
                    Title = $"裸件在库\n({stocksite})",
                    Width = 10
                },
                new()
                {
                    Title = "成品在库\n(C13)",
                    Width = 10
                },
                new()
                {
                    Title = "包装数量",
                    Width = 10,
                    IsColored = true
                },
                new()
                {
                    Title = "包装前\n点检",
                    Width = 10,
                    IsColored = true
                },
                new()
                {
                    Title = $"净重G",
                    Width = 10
                },
                new()
                {
                    Title = $"毛重G",
                    Width = 10
                },
                new()
                {
                    Title = "包装信息/辅材信息",
                    Width = 25
                },
                new()
                {
                    Title = "适配器",
                    Width = 10,
                    IsColored = true
                },
                new()
                {
                    Title = "防伪\n标签",
                    Width = 10,
                    IsColored = true
                },
                new()
                {
                    Title = "说明书",
                    Width = 10,
                    IsColored = true
                },
                new()
                {
                    Title = "照片",
                    Width = 10,
                    IsColored = true
                },
                new()
                {
                    Title = "海外",
                    Width = 10,
                    IsColored = true
                },
                new()
                {
                    Title = "包装完成\n确认",
                    Width = 15,
                    IsColored = true
                },
                new()
                {
                    Title = "品质\n抽检",
                    Width = 15,
                    IsColored = true
                }
            ];

            CreateTableHelper.CreateTitleRowWithOneRow(
                sheet, startRow, titleColumnConfigs, titleRowDefaultStyle, titleRowColoredStyle);

            #endregion

            #region 数据行

            RowInfo dataRowStartRow = new() { Index = 1, Height = 20f };
            List<DataColumnInfo> dataColumnConfigs =
            [
                new() { FieldName = "number" },
                new() { FieldName = "productnumber" },
                new() { FieldName = "daikin_drawingno" },
                new() { FieldName = "name" },
                new() { FieldName = "new_specification" },
                new() { FieldName = "daikin_check_accept" },
                new() { FieldName = "finished_products" },
                new() { IsColored = true },
                new() { IsColored = true },
                new() { FieldName = "daikin_wt" },
                new() { FieldName = "daikin_capacitywt" },
                new() { FieldName = "bc_infos" },
                new() { IsColored = true },
                new() { IsColored = true },
                new() { IsColored = true },
                new() { IsColored = true },
                new() { IsColored = true },
                new() { IsColored = true },
                new() { IsColored = true }
            ];

            CreateTableHelper.CreateDataRow(
                sheet, dataRowStartRow, dataTable, dataColumnConfigs, dataRowDefaultStyle, dataRowColoredStyle);

            #endregion

            var ms = new MemoryStream();
            workbook.Write(ms, true);

            ms.Position = 0;

            return ms;
        }
    }
}
